import requests
from bs4 import BeautifulSoup
import json
from urllib.parse import urljoin

def get_links(url):
    response = requests.get(url)
    response.encoding = 'GBK'
    soup = BeautifulSoup(response.text, 'html.parser')

    # 提取class="c2"的链接
    links = soup.find_all('a', class_='c2')
    links_json = [
        {'text': link.get_text(strip=True), 'url': urljoin(url, link['href'])}
        for link in links if 'href' in link.attrs and link['href'].startswith('/')
    ]

    return links_json

if __name__ == '__main__':
    url = 'https://www.dygang.cc/'
    links_json = get_links(url)
    print(f"Number of links: {len(links_json)}")
    print(json.dumps(links_json, ensure_ascii=False, indent=4))
